package bamin.com.bamindaijia.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.List;

import bamin.com.bamindaijia.R;
import bamin.com.bamindaijia.model.RechargeLog;
import bamin.com.bamindaijia.model.User;

/**
     * 充值记录
     */
  public  class MyTopUpAdapter extends BaseAdapter {
    private final LayoutInflater inflater;
    private List<RechargeLog.ContainsEntity> rechargeLogContains;
    private User mUser;

    public MyTopUpAdapter(Context context, List<RechargeLog.ContainsEntity> rechargeLogContains, User user) {
        inflater = LayoutInflater.from(context);

        this.rechargeLogContains = rechargeLogContains;
        mUser = user;
    }

    class ViewHolder {
            TextView textView_payType;
            TextView textView_payTime;
            TextView textView_payValue;
            TextView textView_chargeAccount;
        }

        @Override
        public int getCount() {
            return rechargeLogContains.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            ViewHolder holder = null;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = inflater.inflate(R.layout.charge_item, null);
                holder.textView_payType = (TextView) convertView.findViewById(R.id.textView_payType);
                holder.textView_payTime = (TextView) convertView.findViewById(R.id.textView_payTime);
                holder.textView_payValue = (TextView) convertView.findViewById(R.id.textView_payValue);
                holder.textView_chargeAccount = (TextView) convertView.findViewById(R.id.textView_chargeAccount);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            String payMode = "";
            RechargeLog.ContainsEntity containsEntity = rechargeLogContains.get(position);
            if (containsEntity.getFlag() == 1) {
                payMode = "微信";
            } else if (containsEntity.getFlag() == 2) {
                payMode = "支付宝";
            }
            if (!TextUtils.equals(containsEntity.getAccountBillPhone(), containsEntity.getRecAccountPhone())) {
                if (TextUtils.equals(mUser.getDatas().getPhone(), containsEntity.getAccountBillPhone())) {
                    holder.textView_chargeAccount.setText(containsEntity.getRecAccountPhone() + "为我充值");
                } else {
                    holder.textView_chargeAccount.setText("为" + containsEntity.getAccountBillPhone() + "充值");
                }
                Log.e("equals",
                        "equals" + containsEntity.getAccountBillPhone() + "\n" + containsEntity.getRecAccountPhone());
                holder.textView_chargeAccount.setVisibility(View.VISIBLE);
            }
            holder.textView_payTime.setText(sdf.format(containsEntity.getDate()));
            holder.textView_payTime.setText(payMode);
            holder.textView_payValue.setText(containsEntity.getPrice() + "元");
            return convertView;
        }
    }